/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.utils;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import systole.ioHandling.logs.SystoleLogger;
import systole.view.MainController;

/**
 *
 * @author user
 */
public class ImageUtils {

    /**
     * Image folder
     */
    public static final String IMAGES_FOLDER = "images";

    /**
     * @param src
     * @param pathResource
     * @return an icon from path
     */
    public static ImageIcon buildIcon(Object src, String pathResource) {

        URL imageURL = src.getClass().getResource(pathResource);
        return new ImageIcon(imageURL);
    }

    /**
     * @param src
     * @param pathResource
     * @return a image from path
     */
    public static Image buildImage(Object src, String pathResource) {
        try {
            URL url = src.getClass().getResource(pathResource);
            return ImageIO.read(url);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param originalImage
     * @param type
     * @param width
     * @param height
     * @return BufferedImage
     */
    public static BufferedImage resizeImage(BufferedImage originalImage, int type, int width, int height) {
        BufferedImage resizedImage = new BufferedImage(width, height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        return resizedImage;
    }

    /**
     * @param relativePath
     * @return BufferedImage
     */
    public static BufferedImage loadImage(String relativePath) {
        try {
            File file = new File(relativePath);
            if (!file.exists()) {
                return null;
            }
            BufferedImage img = ImageIO.read(file);
            return img;
        } catch (IOException ex) {
            SystoleLogger.getInstance().logError("Can not be loaded image file, name " + relativePath);
            return null;
        }
    }

    /**
     * @param im
     * @return BufferedImage
     */
    public static BufferedImage imageToBufferedImage(Image im) {
        BufferedImage bi = new BufferedImage(im.getWidth(null), im.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics bg = bi.getGraphics();
        bg.drawImage(im, 0, 0, null);
        bg.dispose();
        return bi;
    }

    /**
     * @param image
     * @param relativePath
     * @param extension
     */
    public static void saveImage(Image image, String relativePath, String extension) {
        BufferedImage img = imageToBufferedImage(image);
        saveImage(img, relativePath, extension);
    }

    /**
     * @param image
     * @param relativePath
     * @param extension
     */
    public static void saveImage(BufferedImage image, String relativePath, String extension) {
        try {
            File file = new File(relativePath);
            ImageIO.write(image, extension, file);
        } catch (IOException ex) {
            SystoleLogger.getInstance().logError("Can no be saved image file, name " + relativePath);
        }
    }

    /**
     *
     * @param relativePath from systole.view
     * @return icon (null if not exists)
     */
    public static ImageIcon buildIcon(String relativePath) {
        URL imageURL = MainController.class.getResource(relativePath);
        return new ImageIcon(imageURL);
    }

    /**
     *
     * @param relativePath from systole.view
     * @return image (null if not exists)
     */
    public static Image buildImage(String relativePath) {
        try {
            URL url = MainController.class.getResource(relativePath);
            return ImageIO.read(url);
        } catch (Exception e) {
            return null;
        }
    }
}
